<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="搜索">
        <el-input v-model="formInline.userID" @keyup.native.enter="onSubmit" placeholder="请输入用户ID"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit(formInline.userID)">查询</el-button>
      </el-form-item>
    </el-form>
    <el-divider content-position="left">评论记录</el-divider>
    <el-table :data="tableData.result" stripe style="width: 100%">
      <el-table-column prop="user_id" label="用户ID" width="80"> </el-table-column>
      <el-table-column prop="dis_content" label="内容" >
      </el-table-column>
      <el-table-column prop="dis_time" label="时间" width="100"> </el-table-column>
      <el-table-column label="操作" width="180">
        <template v-slot="scope">
          <el-button type="danger" size="small" @click="deleteDis(scope.row)" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

   <el-pagination
        :current-page="query.page"
        :page-sizes="[5,10, 15]"
        :page-size="query.pagesize"
        layout="total, sizes, prev, pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />  
  </div>
</template>

<script>
import { disListAPI, deleDisAPI } from "@/api";

export default {
  data() {
    return {
      formInline: {
        userID:'',
      },
      tableData: {
        result: []
      },
      total: 0,
      query: {
        page: 1,
        pagesize: 10
      }
    };
  },
  mounted() {
    this.getDis()   
  },
  methods: {
    onSubmit (id) {
      if (id) {
        myaxios.post('http://localhost:3500/discuss-id',{user_id:id}).then( res => {
          console.log('用户ID查询评论返回：',res);
          this.tableData.result = res.data.data
        })
      } else {
        this.getDis()
      }
      
    },
    async getDis(){
      const res = await disListAPI(this.query)
      // console.log('评论列表', res);
      this.tableData.result = res.data.data.result;  
      this.total = res.data.data.total   
    },
    handleSizeChange(size) {
      // console.log('每页显示条数', size);
      this.query.pagesize = size
      this.getDis()
    },
    handleCurrentChange(page) {
      // console.log('页数', page);
      this.query.page = page
      this.getDis()
    },
    async deleteDis(data) {
      // console.log('scope.row', data);
      const res = await deleDisAPI({ id: data.dis_id }) 
      // 删除最后一页最后一条数据时，避免请求的页码错误
      if(this.tableData.result.length === 1) {
        this.query.page --
        if(this.query.page === 0) {
          this.query.page = 1
        }
      }
      if (res.data.code === 200) {
        this.$message.success('删除成功!')
        this.getDis()
      }
    },
  },
};
</script>
<style lang="scss" scoped>
</style>